<?php
/**
 * Created by PhpStorm
 * User: lcz
 * Date: 2019/10/21
 * Time: 19:38
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\AgentSendTips;
use app\common\model\FormId;
use think\Db;

class Message extends Api {
	
	
	/**
	 * 我的消息
	 */
	public function index() {
		$userId  = $this->auth->id;
		if($this->auth->level == \app\admin\model\User::LEVEL_USER) {
			$adminLevel = \app\common\model\AgentAdmin::getAdminLevel($userId);
			if(!$adminLevel || $adminLevel['role'] == \app\admin\model\User::LEVEL_USER) {
				$this->error('权限不足');
			}
			$userId = $adminLevel['user_id'];
		}
		$page = $this->request->post('page', 1, 'intval');
		
		$where['user_id'] = $userId;
		$where['status'] = ['neq', '-1'];
		$count = Db::name('message')->where($where)->count();
		$rows = Db::name('message')
			->where($where)
			->order(['create_time' => 'desc'])
			->page($page)
			->column('id, status, image, title, content, from_unixtime(create_time, \'%m-%d %H:%s\') as time', 'id');
		
		if($rows) {
			Db::name('message')->where(['id' => ['in', array_keys($rows)]])->update(['status' => 1]);
		}
		
		$this->success('查询成功', [
			'total' => $count,
			'list' => array_values($rows),
			'page' => $page
		]);
	}
	
	/**
	 * 保存表单ID
	 */
	public function saveFormId(){
		$formId = $this->request->post('formId');
		!$formId && $this->error('参数错误');
		
		$reg = "/mock one/i";
		$match = preg_match($reg,$formId);
		if($match) {
			$this->success('测试数据');
		}
		
		
		$userId = $this->auth->id;
		$model = new FormId();
		
		
		
		$res = $model->allowField(true)
			->isUpdate(false)
			->save([
				'user_id' => $userId,
				'form_id' => $formId,
			]);
		if($res) {
			$this->success('添加成功');
		} else {
			$this->error('添加出错');
		}
	}
	
	
	/**
	 * 未读消息数量
	 */
	public function number() {
		$userId  = $this->auth->id;
		$where['user_id'] = $userId;
		$where['status'] = 0;
		$count = Db::name('message')->where($where)->count();
		
		$this->success('查询成功', $count);
	}
	
	/**
	 * 删除
	 */
	public function delete() {
		$id = $this->request->post('id');
		!$id && $this->error('参数错误');
		$userId = $this->auth->id;
		
		$res = Db::name('message')
			->where(['id' => $id, 'user_id' => $userId])
			->update(['status' => '-1']);
		
		if($res) {
			$this->success('删除成功');
		} else {
			$this->error('删除出错');
		}
	}
	
	
	/**
	 * 发货提醒信息
	 */
	public function sendTips() {
		$userId = $this->auth->id;
		$page = $this->request->post('page', 1, 'intval');
		$status = $this->request->post('status', 0, 'intval');
		
		$where = [
			't.user_id' => $userId,
			't.status' => $status
		];
		
		$count = Db::name('agentSendTips')->alias('t')
			->where($where)
			->count();
		
		$list = Db::name('agentSendTips')->alias('t')
			->join('agentShop a', 't.agent_id = a.id', 'left')
			->join('goods g', 't.goods_id = g.id', 'left')
			->join('goodsSpec s', 't.spec_id = s.id', 'left')
			->where($where)
			->field('t.id, t.agent_id, t.status, a.name, g.name as goods_name, s.name as spec_name, g.image')
			->page($page)
			->select();
		
		$this->success('查询成功', [
			'total' => $count,
			'list' => $list,
			'page' => $page
		]);
	}
	
	/**
	 * 删除发货消息
	 */
	public function delTips() {
		$id = $this->request->post('id');
		!$id && $this->error('参数错误');
		
		$userId = $this->auth->id;
		$res = Db::name('agentSendTips')->where(['id' => $id, 'user_id' => $userId])
			->update(['status' => AgentSendTips::STATUS_DEL, 'update_time' => time()]);
		
		if($res !== false) {
			$this->success('删除成功');
		} else {
			$this->error('删除出错');
		}
	}
}
